﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SyncSPW.SiteSyncPart
{
    [ToolboxItemAttribute(false)]
    public class SiteSyncPart : WebPart
    {
        protected override void RenderContents(HtmlTextWriter writer)
        {
            writer.Write("<script type='text/javascript'>\r\n");
            writer.Write("  function syncSiteSPW(url)\r\n");
            writer.Write("  {\r\n");
            writer.Write("    var OfflineClientScope_Site = 1;\r\n");
            writer.Write("    var OfflineClientScope_ListOrLibrary = 2;\r\n");
            writer.Write("    var OfflineClientScope_Folder = 3;\r\n");
            writer.Write("    var a = new ActiveXObject('Groove.SiteClientActiveX');\r\n");
            writer.Write("    if(a.IsOfflineAllowed(OfflineClientScope_Site, 100, 100, 100))\r\n");
            writer.Write("      a.TakeOffline(OfflineClientScope_Site, 1, url, 1, 1, '', '');\r\n");
            writer.Write("    else\r\n");
            writer.Write("      alert('Site cannot be taken offline.');\r\n");
            writer.Write("  }\r\n");
            writer.Write("</script>\r\n");

            writer.Write("<div>\r\n");
            SPSite siteCollection = SPContext.Current.Site;
            foreach (SPWeb web in siteCollection.AllWebs)
            {
                writer.Write("<a href=\"javascript:syncSiteSPW('");
                writer.Write(web.Url);
                writer.Write("');\">Sync ");
                writer.Write(web.Title);
                writer.Write("</a><br/>\r\n");
                web.Dispose();
            }
            writer.Write("</div>\r\n");
        }
    }
}
